﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Delta
{
    internal class AutoFormChange
    {
        public AutoFormChange(Control mctr)
        {
            MCTR = mctr;
            X = mctr.Width;
            Y = mctr.Height;
            MCTR.Resize += new EventHandler(Form_Resize);
            GETCTRL(MCTR);
        }

        private Control MCTR;
        public float X, Y;

        public void Form_Resize(object sender, EventArgs e)
        {
            float newx = MCTR.Width / X;
            float newy = MCTR.Height / Y;
            SETSIZE(newx, newy, MCTR);
        }

        public void SETSIZE(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ':' });
                    float a = Convert.ToSingle(mytag[0]) * newx;
                    con.Width = (int)a;
                    a = Convert.ToSingle(mytag[1]) * newy;
                    con.Height = (int)(a);
                    a = Convert.ToSingle(mytag[2]) * newx;
                    con.Left = (int)(a);
                    a = Convert.ToSingle(mytag[3]) * newy;

                    con.Top = (int)(a);

                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        SETSIZE(newx, newy, con);
                    }
                }
            }
        }

        public void GETCTRL(Control cons)
        {
            string contxt = "";

            foreach (Control con in cons.Controls)
            {
                contxt = con.ToString();
                con.Tag = con.Width + ":" + con.Height + ":" + con.Left + ":" + con.Top + ":" + con.Font.Size;
                if (con.Controls.Count > 0) GETCTRL(con);
            }
        }
    }
}